
package ru.cod.rs.interfaces.impl;

import ru.cod.rs.interfaces.Game;
import ru.cod.rs.interfaces.Graphics;
import ru.cod.rs.interfaces.Input;
import ru.cod.rs.objects.RenderView;
import ru.cod.rs.objects.Screen;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class AndroidGame extends Activity implements Game {
    
    private Graphics graphics;
    
    private Screen screen;
    
    private RenderView view;
    
    private Input input;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        boolean isLand =
                getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
        
        int w = isLand ? 480 : 320, h = isLand ? 320 : 480;
        Bitmap frame = Bitmap.createBitmap(w, h, Config.RGB_565);
        
        float sX =
                (float) w / getWindowManager().getDefaultDisplay().getWidth();
        float sY =
                (float) h / getWindowManager().getDefaultDisplay().getHeight();
        
        graphics = new GraphicsImpl(getAssets(), frame);
        view = new RenderView(this, frame);
        input = new InputImpl(view, sX, sY);
        screen = getStartScreen();
        setContentView(view);
    }
    
    @Override
    public Graphics getGraphics() {
    
        // TODO Auto-generated method stub
        return graphics;
    }
    
    @Override
    public Screen getCurrentScreen() {
    
        // TODO Auto-generated method stub
        return screen;
    }
    
    @Override
    public Screen getStartScreen() {
    
        // TODO Auto-generated method stub
        return null;
    }
    
    @Override
    public void setScreen(Screen screen) {
    
        this.screen.pause();
        this.screen.dispose();
        screen.resume();
        screen.update(0);
        
        this.screen = screen;
    }
    
    @Override
    public void onPause() {
    
        super.onPause();
        view.pause();
        screen.pause();
        if (isFinishing()) {
            screen.dispose();
        }
    }
    
    @Override
    public void onResume() {
    
        super.onResume();
        screen.resume();
        view.resume();
    }
    
    @Override
    public Input getInput() {
    
        // TODO Auto-generated method stub
        return input;
    }
}
